/* GSManufactureModifyAction2.java
 *
 * Project/Product: JZCG-XMBUS
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   peng.ning	2008-9-28	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import system.LoginContext;
import client.XMLSend;

import com.apep.gw.service.StuationAnalyse;
import com.apep.gw.service.impl.ProductStuationAnalyse;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSProductBidDao;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.form.GSManufactureModForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DateUtil;
import framework.core.SrvContext;

public class GSManufactureModifyAction2 extends GSBaseAction {

    /* (non-Javadoc)
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {
        // TODO Auto-generated method stub
        GSManufactureModForm tForm = (GSManufactureModForm) aForm;
        String backCode = tForm.getRequest().getParameter("concentricity_code_back");
        String newCode = tForm.getConcentricityid();
        GSManufactureProcessor proc = new GSManufactureProcessor();
        StuationAnalyse productStuationAnalyse =new ProductStuationAnalyse();
        try {
            GSManufactureVO vo = new GSManufactureVO();
            //转换为商品bean对象
            vo.setGs_manufacture_id(tForm.getManufacturecode());
            vo.setGs_concentricity_id(tForm.getConcentricityid());
            vo.setGs_manufacture_name(tForm.getManufacturename());
            String category = "";
            String category1 = tForm.getManuftype4();
            String category2 = tForm.getManuftype5();
            String category3 = tForm.getManuftype6();
            SrvContext context = SrvContext.getContext(aForm.getRequest());
            LoginContext loginInfo = LoginContext.get(context);
            //添加产品种类
            if(category2.equals("0")){
                category = category1;
            }else if(category3.equals("0")){
                category = category2;
            }else{
                category = category3;
            }
            String productType = tForm.getProducttype();
            String productBid = tForm.getProductbid();

                vo.setGs_manufacture_category_id(category);
                vo.setGs_manufacture_brand(tForm.getManufacturebrand());
                vo.setGs_manufacture_type(tForm.getManufacturetype());
                vo.setGs_manufacture_spec(tForm.getManfuacturespec());
                vo.setGs_manufacture_unitprice(String.valueOf(Double.parseDouble(tForm.getUnitprice())));
                vo.setGs_quant_unit(tForm.getQuantunit());
                vo.setGs_quality_assure_period(tForm.getQualityassureperiod());
                vo.setGs_manufacturer_name(tForm.getManufacturername());
                vo.setGs_make_area(tForm.getMakearea());
                vo.setGs_provider_id(tForm.getProviderid());
                vo.setGs_adapt_machine_type(tForm.getMachinetype());
                vo.setGs_appender_property(tForm.getProperty());
                vo.setGs_remark(tForm.getRemark());
                vo.setIs_buy(tForm.getIsbuy2());
                vo.setGs_drawing_peice_code(tForm.getDrawingpiececode2());
                vo.setGsValidity("1");//有效产品
                vo.setGs_is_tradeflat_manufacture("1");
                vo.setGs_balance_mode(tForm.getMode2());
                vo.setKm_quality_assure_period(tForm.getKmqualityassureperiod());
                vo.setLast_update_time(new DateUtil().getFullString());
                vo.setLast_update_user_id(loginInfo.getLoginUserId());
                vo.setLast_update_user(loginInfo.getLoginUserName());
                vo.setBalance_customer_code(tForm.getBalancecustomercode());
                vo.setPic(tForm.getPic());
            if(GSBaseConstants.PLANPRICE.equals("true")){
                vo.setGs_planprice(tForm.getPlanprice());
            }else{
                vo.setGs_planprice(String.valueOf(Double.parseDouble(tForm.getUnitprice())));
            }
            boolean bl = false;
            if(!backCode.equals(newCode)){
                if (newCode.trim().length()!=GSBaseConstants.PRODUCT_CONCENTRICITY&&Integer.parseInt(GSSystemConfigurator.getParam("gs.is.concentricity"))==0) {
                    throw new GSException("集中编码长度必须为"+GSBaseConstants.PRODUCT_CONCENTRICITY+"编码,集中编码："+newCode);
                }
                GSManufactureBean mbean=proc.getManufInfoByConcentricityId(newCode.trim());
                if (mbean!=null) {
                    throw new GSException("已存在相同集中编码的产品信息,集中编码："+newCode.trim());
                }
                vo.setGs_manufacture_id(proc.getManufactureId());
                vo.setGs_recorder_id(loginInfo.getUser().getCode());
                vo.setGs_manufacture_record_date(new DateUtil().getFullString());
                vo.setBalance_customer_code("0");
               // vo.setPic(" ");
                vo.setRecent_adjustprice_date(new DateUtil().getFullString());
                vo.setRecent_adjusted_price(String.valueOf(Double.parseDouble(tForm.getUnitprice())));
                vo.setIs_normal(GSBaseConstants.IS_NORMAL);
                vo.setWarehouse_code(ConstDefine.T_WARE_HOUSE_CODE);
                vo.setWarehouse_name(ConstDefine.T_WAREE_HOUSE_NAME);
                vo.setCreate_time(new DateUtil().getFullString());
                vo.setCreate_user(loginInfo.getLoginUserName());
                vo.setCreate_user_id(loginInfo.getLoginUserId());
                vo.setIsupdate(GSBaseConstants.IS_UPDATE);

                if(GSBaseConstants.PLANPRICE.equals("true")){
                    vo.setGs_planprice(tForm.getPlanprice());
                }else{
                    vo.setGs_planprice(String.valueOf(Double.parseDouble(tForm.getUnitprice())));
                }
                
                GSManufactureBean mbean2=proc.getManufInfoByApepCode(tForm.getManufacturecode());
                if(GSBaseConstants.IS_CENTER_WAREHOUSE!=null&&GSBaseConstants.IS_CENTER_WAREHOUSE.equals("true")){
                    vo.setGs_planprice(mbean2.getPlan_price()+"");
                }
                
                bl = proc.updateProductKM(tForm.getManufacturecode(), vo);
                
                
                
                //新插入修改后数据
                doProductBussinessInsert(tForm, proc, productStuationAnalyse, vo,
						loginInfo, productType, productBid, newCode);
                
      
                if(mbean2!=null){
                	 GSProductBidDao bidDao = new GSProductBidDao();
                	 	GSProductTypeDao dao = new GSProductTypeDao();
                     GSProductTypeBean bean = dao.findProductTypeByProuductCodeNoStauts(mbean2.getGs_concentricity().trim());
                     if(bean !=null){
                   	  	dao.updatePrudctTypeStatusByProductCode(mbean2.getGs_concentricity().trim() );
                     }
                    
                     GSProductBidBean bidBean = bidDao.findProductBidByProuductCodeNoStauts(mbean2.getGs_concentricity().trim());
                     if(bidBean !=null){
                   	  bidDao.updatePrudctBidStatusByProductCode(mbean2.getGs_concentricity().trim());
                     }
                }
               
            }else{
                vo.setIsupdate(GSBaseConstants.IS_UPDATE);
                GSManufactureBean mbean=proc.getManufInfoByConcentricityId(vo.getGs_concentricity_id().trim());
                if (mbean==null) {
                    throw new GSException("集中编码："+vo.getGs_concentricity_id()+"不存在");
                }
                if(GSBaseConstants.IS_CENTER_WAREHOUSE!=null&&GSBaseConstants.IS_CENTER_WAREHOUSE.equals("true")){
                    vo.setGs_planprice(mbean.getPlan_price()+"");
                }
                bl = proc.modifyTask2KM(vo);

                doProductBussinessUpdete(tForm, proc, productStuationAnalyse, vo,
						loginInfo, productType, productBid, mbean);

            }
            // 修改产品信息
            if(bl){
                aForm.getRequest().setAttribute("message", "修改产品执行成功");
                /*  if (XMLSend.getInstance().isToRong()) {
                    String actionName = "SubmitMaterialInfo";
                    String dataName = "MaterialInfo";
                    // 传输xml to 荣爱
                 XMLSend.getInstance().submitMaterialInfoSendXML(actionName, dataName, vo);

                } */
            }else{
                throw new GSException("修改产品执行失败");
            }
        } catch (Exception e) {
            aForm.getRequest().setAttribute("action", "A400301.do");
            GSLogWriter.error(this, e.toString());
            throw e;
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

	private void doProductBussinessUpdete(GSManufactureModForm tForm,
			GSManufactureProcessor proc,
			StuationAnalyse productStuationAnalyse, GSManufactureVO vo,
			LoginContext loginInfo, String productType, String productBid,GSManufactureBean mbean) throws GSException {
		GSProductTypeDao dao = new GSProductTypeDao();
		GSProductBidDao bidDao = new GSProductBidDao();
		
		if(vo.getIs_buy().equals("1")){
			if(mbean.getIs_buy().equals("0")){
				productStuationAnalyse.open(proc.appendUpdateIsBuy(mbean, vo), vo.getLast_update_time(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(),mbean!=null?mbean.getId():"");
			}
			if(vo.getGs_manufacture_unitprice()!=null&&!vo.getGs_manufacture_unitprice().equals("")&&Double.parseDouble(vo.getGs_manufacture_unitprice())!=mbean.getGs_manufacture_unitprice()){
				productStuationAnalyse.changePrice(proc.appendUpdatePrice(mbean, vo), vo.getLast_update_time(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(),mbean!=null?mbean.getId():"");
			}
			if(!"".equals(proc.appendUpdateContent(mbean, vo))){
				   productStuationAnalyse.update(proc.appendUpdateContent(mbean, vo), vo.getLast_update_time(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(),mbean!=null?mbean.getId():"");
			}
		 
		    GSProductTypeBean bean = dao.findProductTypeByProuductCodeNoStauts(tForm.getConcentricityid());

		    if(bean ==null){
		    		bean = new GSProductTypeBean();
		        	bean.setCreateTime(new DateUtil().getFullString());
		        	bean.setCreateUser(loginInfo.getLoginUserName());
		        	bean.setCreateUserId(loginInfo.getLoginUserId());
		        	bean.setIsBalance(1);
		        	bean.setIsBid(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		        	bean.setLastUpdateTime(new DateUtil().getFullString());
		        	bean.setLastUpdateUser(loginInfo.getLoginUserName());
		        	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
		        	bean.setProductCode(tForm.getConcentricityid());
		        	bean.setProductType(productType.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productType));
		        	bean.setProviderCode(tForm.getProviderid());
		        	bean.setProductId(mbean.getId());
		        	bean.setRemark("");
		        	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		        	dao.insert(bean);
		    }else{
		    	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		    	bean.setProductCode(tForm.getConcentricityid());
		    	bean.setProductType(productType.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productType));
		    	bean.setIsBid(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		    	bean.setProviderCode(tForm.getProviderid());
		    	bean.setLastUpdateTime(new DateUtil().getFullString());
		    	bean.setLastUpdateUser(loginInfo.getLoginUserName());
		    	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
		    	dao.update(bean);
		    }

		    		GSProductBidBean bidBean = bidDao.findProductBidByProuductCode(tForm.getConcentricityid());
		            if(bidBean !=null){
		            	bidBean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		            	bidBean.setProductId(mbean.getId());
		            	bidBean.setProductCode(vo.getGs_concentricity_id().trim());
		            	bidBean.setProviderCode(vo.getGs_provider_id());
		            	bidBean.setProductType(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		            	bidBean.setLastUpdateTime(new DateUtil().getFullString());
		            	bidBean.setLastUpdateUser(loginInfo.getLoginUserName());
		            	bidBean.setLastUpdateUserId(loginInfo.getLoginUserId());
		            	bidDao.update(bidBean);
		            }else{
		    	      	bidBean = new GSProductBidBean();
		    	      	bidBean.setCreateTime(new DateUtil().getFullString());
		    	      	bidBean.setCreateUser(loginInfo.getLoginUserName());
		    	      	bidBean.setCreateUserId(loginInfo.getLoginUserId());
		    	      	bidBean.setProductName(vo.getGs_manufacture_name());
		    	      	bidBean.setLastUpdateTime(new DateUtil().getFullString());
		    	      	bidBean.setLastUpdateUser(loginInfo.getLoginUserName());
		    	      	bidBean.setLastUpdateUserId(loginInfo.getLoginUserId());
		    	      	bidBean.setProductCode(vo.getGs_concentricity_id().trim());
		    	      	bidBean.setProductType(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		    	      	bidBean.setProviderCode(vo.getGs_provider_id().trim());
		    	      	bidBean.setProductId(mbean.getId());
		    	      	bidBean.setRemark("");
		    	      	bidBean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		    	      	bidDao.insert(bidBean);
		            }

		    
		}else{
		    productStuationAnalyse.close(proc.appendUpdateIsBuy(mbean, vo), vo.getLast_update_time(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(), mbean.getId());

			  GSProductTypeBean bean = dao.findProductTypeByProuductCodeNoStauts(tForm.getConcentricityid().trim());
		      if(bean !=null){
		    	  dao.updatePrudctTypeStatusByProductCode(tForm.getConcentricityid().trim());
		      }
		    
		      GSProductBidBean bidBean = bidDao.findProductBidByProuductCodeNoStauts(tForm.getConcentricityid().trim());
		      if(bidBean !=null){
		    	  bidDao.updatePrudctBidStatusByProductCode(tForm.getConcentricityid().trim());
		      }
		}
	}
	private void doProductBussinessInsert(GSManufactureModForm tForm,
			GSManufactureProcessor proc,
			StuationAnalyse productStuationAnalyse, GSManufactureVO vo,
			LoginContext loginInfo, String productType, String productBid,
			String newCode) throws GSException {
		GSProductTypeDao dao = new GSProductTypeDao();
		GSProductBidDao bidDao = new GSProductBidDao();
		GSManufactureBean mbean=proc.getManufInfoByConcentricityId(newCode.trim());
		if(vo.getIs_buy().equals("1")){
		    productStuationAnalyse.insert(proc.appendInsertContent(vo), vo.getLast_update_time(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(), mbean!=null?mbean.getId():"");
		    GSProductTypeBean bean = dao.findProductTypeByProuductCodeNoStauts(tForm.getConcentricityid());

		    if(bean ==null){
		    		bean = new GSProductTypeBean();
		        	bean.setCreateTime(new DateUtil().getFullString());
		        	bean.setCreateUser(loginInfo.getLoginUserName());
		        	bean.setCreateUserId(loginInfo.getLoginUserId());
		        	bean.setIsBalance(1);
		        	bean.setIsBid(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		        	bean.setLastUpdateTime(new DateUtil().getFullString());
		        	bean.setLastUpdateUser(loginInfo.getLoginUserName());
		        	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
		        	bean.setProductCode(tForm.getConcentricityid());
		        	bean.setProductType(productType.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productType));
		        	bean.setProviderCode(tForm.getProviderid());
		        	bean.setProductId(mbean.getId());
		        	bean.setRemark("");
		        	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		        	dao.insert(bean);
		    }else{
		    	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		    	bean.setProductCode(tForm.getConcentricityid());
		    	bean.setProductType(productType.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productType));
		    	bean.setIsBid(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		    	bean.setProviderCode(tForm.getProviderid());
		    	bean.setLastUpdateTime(new DateUtil().getFullString());
		    	bean.setLastUpdateUser(loginInfo.getLoginUserName());
		    	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
		    	dao.update(bean);
		    }
		
		    		GSProductBidBean bidBean = bidDao.findProductBidByProuductCode(tForm.getConcentricityid());
		            if(bidBean !=null){
		            	bidBean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		            	bidBean.setProductId(mbean.getId());
		            	bidBean.setProductCode(vo.getGs_concentricity_id().trim());
		            	bidBean.setProviderCode(vo.getGs_provider_id());
		            	bidBean.setProductType(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		            	bidBean.setLastUpdateTime(new DateUtil().getFullString());
		            	bidBean.setLastUpdateUser(loginInfo.getLoginUserName());
		            	bidBean.setLastUpdateUserId(loginInfo.getLoginUserId());
		            	bidDao.update(bidBean);
		            }else{
		    	      	bidBean = new GSProductBidBean();
		    	      	bidBean.setCreateTime(new DateUtil().getFullString());
		    	      	bidBean.setCreateUser(loginInfo.getLoginUserName());
		    	      	bidBean.setCreateUserId(loginInfo.getLoginUserId());
		    	      	bidBean.setProductName(vo.getGs_manufacture_name());
		    	      	bidBean.setLastUpdateTime(new DateUtil().getFullString());
		    	      	bidBean.setLastUpdateUser(loginInfo.getLoginUserName());
		    	      	bidBean.setLastUpdateUserId(loginInfo.getLoginUserId());
		    	      	bidBean.setProductCode(vo.getGs_concentricity_id().trim());
		    	      	bidBean.setProductType(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
		    	      	bidBean.setProviderCode(vo.getGs_provider_id().trim());
		    	      	bidBean.setProductId(mbean.getId());
		    	      	bidBean.setRemark("");
		    	      	bidBean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
		    	      	bidDao.insert(bidBean);
		            }		    
		}else{
		    productStuationAnalyse.close(proc.appendUpdateIsBuy(mbean, vo), vo.getLast_update_time(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(), mbean.getId());

			  GSProductTypeBean bean = dao.findProductTypeByProuductCodeNoStauts(tForm.getConcentricityid().trim());
		      if(bean !=null){
		    	  dao.updatePrudctTypeStatusByProductCode(tForm.getConcentricityid().trim());
		      }
		    
		      GSProductBidBean bidBean = bidDao.findProductBidByProuductCodeNoStauts(tForm.getConcentricityid().trim());
		      if(bidBean !=null){
		    	  bidDao.updatePrudctBidStatusByProductCode(tForm.getConcentricityid().trim());
		      }
		}
	}
}
